package org.cresource.pet.controller;


import io.swagger.annotations.ApiOperation;
import org.cresource.basic.util.AjaxResult;
import org.cresource.basic.util.LoginContext;
import org.cresource.basic.util.PageList;
import org.cresource.pet.domain.Pet;
import org.cresource.pet.domain.PetDetail;
import org.cresource.pet.query.PetQuery;
import org.cresource.pet.service.IPetService;
import org.cresource.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    /**
     * 点击购买 进入详情页
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "服务查询 查询单独的服务",notes = "需要参数:服务的Id")
    public Pet getById(@PathVariable("id") Long id){
        return  petService.getById(id);
    }














    /**
     * 页面数据          直接走basic
     * 页面数据
     * 页面数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "需要参数:currentPage和PageSize")
    public PageList<Pet> queryPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }
    /**
     * 批量修改上下架
     * 批量修改上下架
     * 批量修改上下架
     */
    @PostMapping("/onoroffsale")
    @ApiOperation(value = "批量修改上下架",notes = "需要参数:ids")
    public AjaxResult onOrOffSale(@RequestBody Map<String,Object> map){

        try {
            petService.onoroffsale(map);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败,请稍后重试");
        }
    }
    /**
     * 前端主站使用的接口
     *   只查询上架的
     * 前端主站使用的接口
     *   只查询上架的
     * 前端主站使用的接口
     *   只查询上架的
     */
    @PostMapping("/list")
    public PageList<Pet> queryWebPage(@RequestBody PetQuery petQuery){
        petQuery.setState(1);
        System.out.println(petQuery+"******************************");
        return petService.queryPage(petQuery);
    }



    /**
     * 领养领养
     * 领养领养
     * 领养领养
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable(value = "id")Long petId, HttpServletRequest request){
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        try {
            petService.adopt(petId,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("不允许包养！"+e.getMessage());
        }
    }


















    /**
     * 回显
     */
    @GetMapping("/detail/{productId}")
    @ApiOperation(value = "查询一条服务数据",notes = "需要传入productId")
    public PetDetail getDetailByProductId(@PathVariable("productId") Long petId){
        return petService.getDetailByPetId(petId);
    }
    /**
     * 添加或者修改
     * 添加或者修改
     * 添加或者修改
     */
    @PutMapping
    @ApiOperation(value = "服务添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){
                pet.setState(0);
                petService.add(pet);
            }else {
                petService.update(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult.me().setMessage("系统繁忙,请稍后重试");
        }
        return AjaxResult.me();
    }




}
